import Vue from "vue";
import VueRouter from "vue-router";
import myList from "./myList.js";
import indexList from "./indexList.js";
import store from "../store";
import reservation from "./reservation.js";
import record from "./record.js";
Vue.use(VueRouter);

const routes = [
  // 根页面
  {
    path: "/login",
    name: "login",
    component: () =>
      import(/* webpackChunkName: "auth" */ "../views/Login.vue"),
  },
  // 注册
  {
    path: "/register",
    name: "register",
    component: () =>
      import(/* webpackChunkName: "auth" */ "../views/Register.vue"),
  },
  {
    path: "/",
    component: () => import(/* webpackChunkName: "root" */ "../views/Root.vue"),
    children: [
      {
        path: "",
        redirect: "/home",
      },
      // 首页
      {
        path: "home",
        name: "home",
        component: () =>
          import(/* webpackChunkName: "home" */ "../views/Home/Index.vue"),
      },
      ...indexList,
      ...reservation,
      ...record,
      // 消息
      {
        path: "msg",
        name: "msg",
        component: () =>
          import(/* webpackChunkName: "msg" */ "../views/Msg/Index.vue"),
      },
      // 聊天
      {
        path: "chat",
        name: "chat",
        component: () =>
          import(
            /* webpackChunkName: "notifications" */
            "../views/Msg/com/chat.vue"
          ),
      },
      // 系统通知
      {
        path: "systemNotifications",
        name: "systemNotifications",
        component: () =>
          import(
            /* webpackChunkName: "notifications" */
            "../views/Msg/com/systemNotifications.vue"
          ),
      },
      // 我的
      {
        path: "mine",
        name: "mine",
        component: () =>
          import(/* webpackChunkName: "mine" */ "../views/Mine/Index.vue"),
      },
      ...myList,
    ],
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  const { name } = to;
  //直接进入登录/注册
  if (name === "login" || name === "register") {
    next();
  }
  //其他页面，需要判断用户是否登录了。
  else {
    if (store.state.user.isLogin) {
      next(); //登录了，直接进入
    } else {
      next("/login");
    }
  }
});
export default router;
